<?php

declare(strict_types=1);

namespace app\index\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Like extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
    public function article()
    {
        return $this->belongsTo(Article::class, 'article_id', 'article_id');
    }

    public static function getLikeInfo($article_id = null, $user_id = null)
    {
        // 获取文章的总点赞数
        $like_count = self::where('article_id', $article_id)->count();

        // 获取当前用户的点赞状态
        $is_liked = false;
        $is_liked = self::where([
            'article_id' => $article_id,
            'user_id' => $user_id
        ])->value('status');

        return [
            'likeCounts' => $like_count,
            'likeStatus' => $is_liked
        ];
    }
}
